package edu.ubb.remeye.server.screen.capture;

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import edu.ubb.remeye.exception.ScreenCaptureException;
import edu.ubb.remeye.gui.internationalization.LanguageManager;

/**
 * Desktop capturer class.
 * 
 * @author Andrew Ianicicau
 *
 */
public class DesktopScreenCapturer implements ScreenCapturer {

	///////////////////////////////////////////////////////////////////////////////////
	////		ATTRIBUTES - DesktopScreenCapturer
	///////////////////////////////////////////////////////////////////////////////////
	
	/**
     * Captured image of desktop screen.
     */
    private BufferedImage desktopImage = null;
    
    /**
     * Dimension of desktop.
     */
    private Dimension desktopSize = null;
    
    /**
     * Robot for capturing the desktop screen.
     */
    private Robot robot = null;
    

	
	///////////////////////////////////////////////////////////////////////////////////
	////		CONSTRUCTORS - DesktopScreenCapturer
	///////////////////////////////////////////////////////////////////////////////////

    public DesktopScreenCapturer() throws ScreenCaptureException {
        desktopSize = Toolkit.getDefaultToolkit().getScreenSize();
        try {
			robot = new Robot();
		} catch (AWTException e) {
			String errorMessage = LanguageManager.getLanguageManager().getText("captureerror");
			throw new ScreenCaptureException(errorMessage);
		}
    }

	
	
	///////////////////////////////////////////////////////////////////////////////////
	////		METHODS
	///////////////////////////////////////////////////////////////////////////////////
	
    /**
     * Captures the desktop screen using the Robot.
     */
    @Override
    public BufferedImage capture() {
        desktopImage = robot.createScreenCapture(new Rectangle(desktopSize));
        return desktopImage;
    }
    
}
